/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import database_beans.Korisnik;
import database_beans.Role;
import database_beans.Student;
import database_beans.Zaposleni;
import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.primefaces.event.FileUploadEvent;
import server.Util;

/**
 *
 * @author stefan
 */
@ManagedBean
@ViewScoped
public class AdminDodajOsobu implements Serializable{

    private Korisnik korisnik;
    private Student student;
    private Zaposleni zaposlen;
    private String reeEnteredPassword;
    private boolean hasUser = false;
    private Role studentRole;
    private Role zaposleniRole;
    byte[] slikaData;
    public String reenteredPassword;
    
    private ArrayList<Student> studentList;
    private ArrayList<Zaposleni> zaposleniList;
    
    private Student selectedStudent;
    private Zaposleni selectedZaposleni;
    
    private String selectedStudentString;
    private String selectedZaposleniString;
    
    boolean imaStudent;
    boolean imaZaposleni;

    public AdminDodajOsobu() {

    }

    @PostConstruct
    public void init() {
        korisnik = new Korisnik();
        this.student = new Student();
        this.zaposlen = new Zaposleni();
        loadRoles();
        loadStudenti();
        loadZaposleni();
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Zaposleni getZaposlen() {
        return zaposlen;
    }

    public void setZaposlen(Zaposleni zaposlen) {
        this.zaposlen = zaposlen;
    }

    public void dodajStudenta() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            korisnik.setRole(studentRole);
            korisnik.setStudent(student);
            student.setKorisnik(korisnik);
             session.save(student);
            session.save(korisnik);                   
            t.commit();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Student snimljen!"));
            korisnik = new Korisnik();
            student = new Student();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska!!! Student nije snimljen!!"));
        }
    }

    public void dodajZaposlenog() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            korisnik.setRole(zaposleniRole);
            zaposlen.setSlika(slikaData);
            korisnik.setZaposleni(zaposlen);
            zaposlen.setKorisnik(korisnik);
            session.save(korisnik);            
            session.save(zaposlen);
            t.commit();
            korisnik = new Korisnik();
            zaposlen = new Zaposleni();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Zaposleni snimljen!"));
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska!!! Zaposleni nije snimljen!!"));
        }

    }
    
    public void azirirajStudenta() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        
        try {
            session.update(selectedStudent.getKorisnik());            
            session.update(selectedStudent);
            t.commit();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Student snimljen!"));
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska!!! Student nije snimljen!!"));
        }
    }
    
    public void azirirajZaposlenog() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            session.update(selectedZaposleni.getKorisnik());
            if(slikaData != null){
                selectedZaposleni.setSlika(slikaData);
            }
            session.update(selectedZaposleni);
            t.commit();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Zaposleni snimljen!"));
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska!!! Zaposleni nije snimljen!!"));
        }
    }

    private void checkUser() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria c = session.createCriteria(Korisnik.class);
            c.add(Restrictions.eq("korisnickoIme", korisnik.getKorisnickoIme()));
            c.add(Restrictions.eq("lozinka", korisnik.getLozinka()));
            if (!c.list().isEmpty()) {
                hasUser = true;
            }
            t.commit();
        } catch (Exception e) {
            t.rollback();
        }
    }

    private void loadRoles() {
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria c = session.createCriteria(Role.class);
            ArrayList<Role> list = (ArrayList<Role>) c.list();

            for (Role r : list) {
                if ("student".equals(r.getDescription())) {
                    studentRole = r;
                } else if ("zaposleni".equals(r.getDescription())) {
                    zaposleniRole = r;
                }
            }
            t.commit();
        } catch (Exception e) {
            t.rollback();
        }
    }
    
    private void loadStudenti(){
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria c = session.createCriteria(Student.class);
            studentList = (ArrayList<Student>) c.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
        }
    }
    
    private void loadZaposleni(){
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria c = session.createCriteria(Zaposleni.class);
            zaposleniList = (ArrayList<Zaposleni>) c.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
        }
    }
    
    public void onChangeStudent() {
        selectedStudent = new Student();
        imaStudent = false;
        if (!"".equals(selectedStudentString)) {
            for (Student s : studentList) {
                if (selectedStudentString.equals(s.getKorisnik().getIme() + " " + s.getKorisnik().getPrezime())) {
                    selectedStudent = s;
                    imaStudent = true;
                    break;
                }
            }
        }
    }
    
    public void onChangeZaposleni() {
        selectedZaposleni = new Zaposleni();
        imaZaposleni = false;
        if (!"".equals(selectedZaposleniString)) {
            for (Zaposleni s : zaposleniList) {
                if (selectedZaposleniString.equals(s.getKorisnik().getIme() + " " + s.getKorisnik().getPrezime())) {
                    selectedZaposleni = s;
                    imaZaposleni = true;
                    break;
                }
            }
        }
    }

    public ArrayList<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(ArrayList<Student> studentList) {
        this.studentList = studentList;
    }

    public ArrayList<Zaposleni> getZaposleniList() {
        return zaposleniList;
    }

    public void setZaposleniList(ArrayList<Zaposleni> zaposleniList) {
        this.zaposleniList = zaposleniList;
    }

    public Student getSelectedStudent() {
        return selectedStudent;
    }

    public void setSelectedStudent(Student selectedStudent) {
        this.selectedStudent = selectedStudent;
    }

    public Zaposleni getSelectedZaposleni() {
        return selectedZaposleni;
    }

    public void setSelectedZaposleni(Zaposleni selectedZaposleni) {
        this.selectedZaposleni = selectedZaposleni;
    }

    public String getSelectedStudentString() {
        return selectedStudentString;
    }

    public void setSelectedStudentString(String selectedStudentString) {
        this.selectedStudentString = selectedStudentString;
    }

    public String getSelectedZaposleniString() {
        return selectedZaposleniString;
    }

    public void setSelectedZaposleniString(String selectedZaposleniString) {
        this.selectedZaposleniString = selectedZaposleniString;
    }

    public boolean isImaStudent() {
        return imaStudent;
    }

    public void setImaStudent(boolean imaStudent) {
        this.imaStudent = imaStudent;
    }

    public boolean isImaZaposleni() {
        return imaZaposleni;
    }

    public void setImaZaposleni(boolean imaZaposleni) {
        this.imaZaposleni = imaZaposleni;
    }

    public String getReenteredPassword() {
        return reenteredPassword;
    }

    public void setReenteredPassword(String reenteredPassword) {
        this.reenteredPassword = reenteredPassword;
    }

    public void uploadHandlerSlika(FileUploadEvent event) {
        slikaData = event.getFile().getContents();
    }

    public Korisnik getKorisnik() {
        return korisnik;
    }

    public void setKorisnik(Korisnik korisnik) {
        this.korisnik = korisnik;
    }

    public String getReeEnteredPassword() {
        return reeEnteredPassword;
    }

    public void setReeEnteredPassword(String reeEnteredPassword) {
        this.reeEnteredPassword = reeEnteredPassword;
    }

    public boolean isHasUser() {
        return hasUser;
    }

    public void setHasUser(boolean hasUser) {
        this.hasUser = hasUser;
    }

    public Role getStudentRole() {
        return studentRole;
    }

    public void setStudentRole(Role studentRole) {
        this.studentRole = studentRole;
    }

    public Role getZaposleniRole() {
        return zaposleniRole;
    }

    public void setZaposleniRole(Role zaposleniRole) {
        this.zaposleniRole = zaposleniRole;
    }

    public byte[] getSlikaData() {
        return slikaData;
    }

    public void setSlikaData(byte[] slikaData) {
        this.slikaData = slikaData;
    }

}
